package cn.wsalix.user.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Observable;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.service.impl.BasicServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.form.UserForm;
import cn.wsalix.observable.service.LoginService;
import cn.wsalix.observable.service.RegisterService;
import cn.wsalix.shiro.token.QQToken;
import cn.wsalix.shiro.token.WxAppToken;
import cn.wsalix.user.entity.UserCenter;
import cn.wsalix.user.form.UserCenterForm;
import cn.wsalix.user.repository.UserCenterRepository;
import cn.wsalix.user.service.UserCenterService;
import cn.wsalix.utils.UserUtils;
import cn.wsalix.weixin.entity.WxUser;

@Service("userCenterService")
public class UserCenterServiceImpl extends
		BasicServiceImpl<UserCenterForm, UserCenterRepository, UserCenter>
		implements UserCenterService {
	@Autowired
	private UserCenterRepository userCenterRepository;
	/*
	 * @Autowired private UserService userService;
	 */
	@Autowired
	protected UserUtils userUtils;

	@Override
	public String module() {
		return "/user/center";
	}

	@Override
	public UserCenterRepository getRepository() {
		return userCenterRepository;
	}

	@Override
	public Class<UserCenter> getEntityClazz() {
		return UserCenter.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(UserCenterForm entity) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (entity.getUserId() != null && entity.getUserId() != 0) {
			filters.put("user.id", new SearchFilter("user.id", Operator.EQ,
					entity.getUserId()));
		}
		return filters;
	}

	@Transactional
	@Override
	public UserCenter findByUser(SysUser user) {
		UserCenter obj = userCenterRepository.findOne(user.getId());
		if (obj == null) {
			obj = initUserCenter(new UserCenterForm(), user);
		}
		return obj;
	}

	@Transactional
	private UserCenter initUserCenter(UserCenterForm userCenterForm,
			SysUser user) {
		UserCenter userCenter = new UserCenter();
		userCenter.setUser(user);
		userCenter.setAmount(0);
		userCenter.setIntegral(0);
		userCenter = userCenterRepository.save(userCenter);
		return userCenter;
	}

	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof RegisterService) {
			RegisterService userRegisterService = (RegisterService) o;
			if (arg instanceof UserForm) {
				UserForm form = (UserForm) arg;
				SysUser user = form.getUser();
				UserCenterForm userCenterForm = new UserCenterForm();
				initUserCenter(userCenterForm, user);
				// SysUser user = (SysUser) arg;
				/*
				 * UserCenter userCenter = new UserCenter();
				 * userCenter.setUser(user); userCenter.setAmount(0);
				 * userCenter.setIntegral(0);
				 * userCenterRepository.save(userCenter);
				 */
			}
		}
		if (o instanceof LoginService) {
			BaseUser baseUser = userUtils.getUser();
			if (baseUser != null) {// 用户已登录，自动绑定账号
				return;
			}
			LoginService loginService = (LoginService) o;
			SysUser user = loginService.getUser();
			Subject subject = SecurityUtils.getSubject();
			UserCenter obj = userCenterRepository.findByUserIdOne(user.getId());
			if (arg instanceof QQToken) {

				QQToken upToken = (QQToken) arg;
				// QQUserForm qqUserForm = upToken.getQqUserForm();

				if (obj == null) {
					obj = new UserCenter();
					obj.setUser(user);
					userCenterRepository.save(obj);
				}
				if (user.getUserCenter() == null) {
					loginService.setUserCenter(obj);
				}
				subject.login(upToken);
			}
			if (arg instanceof WxAppToken) {
				user = loginService.getUser();
				WxAppToken upToken = (WxAppToken) arg;
				WxUser wxUser = upToken.getWxUser();
				/*
				 * UserCenter obj = userCenterRepository.findByUserIdOne(user
				 * .getId());
				 */
				if (obj == null) {
					obj = new UserCenter();
					obj.setUser(user);
					userCenterRepository.save(obj);
				}
				if (user.getUserCenter() == null) {
					loginService.setUserCenter(obj);
				}
				subject.login(upToken);
			}

		}

	}

}
